package com.example.yunhailutong.zhongtan.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.example.yunhailutong.zhongtan.R;
import com.example.yunhailutong.zhongtan.app.App;
import com.example.yunhailutong.zhongtan.app.OldsEvent;
import com.example.yunhailutong.zhongtan.entity.LoginEntity;
import com.example.yunhailutong.zhongtan.net.PersionBiz;
import com.example.yunhailutong.zhongtan.util.SharedPreferencesUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yunhailutong on 2018/4/26.
 */

public class ActivityLogin extends ActivityBase {
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.login)
    Button login;
    @BindView(R.id.register)
    TextView register;
    @BindView(R.id.zanbuzhuce)
    TextView zanbuzhuce;
    @BindView(R.id.forgetPassword)
    TextView forgetPassword;

    @Override
    protected void addListener() {
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (phone.getText().toString().equals("")) {
                    disPlay("请输入手机号");
                } else if (password.getText().toString().equals("")) {
                    disPlay("请输入密码");
                } else {
                    login();
                }
            }
        });
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(ActivityRegister.class);
            }
        });

        zanbuzhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                openActivity(MainActivity.class);
                finish();
            }
        });

        forgetPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("Leon", "********forgetPassword********");
            }
        });
    }

    public void loginSucess() {
        Intent intent = new Intent(getActivity(), MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        startActivity(intent);
    }

    @Override
    protected void initActivity(Bundle savedInstanceState) {
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);
    }

    @Override
    protected int contentViewById() {
        return R.layout.activity_login;
    }

    public void login() {
        showWaitingDialog();
        Map<String, String> params = new HashMap<>();
        params.put("phone", phone.getText().toString());
        params.put("pwd", password.getText().toString());
//        params.put("phone", "18765396874");
//        params.put("pwd","123456");
        new PersionBiz().login(params);
    }

    @Subscribe
    public void log(OldsEvent.LoginEvent msg) {
        dismissWaitingDialog();
        if (msg.what == -1) {
            disPlay(msg.obj.toString());
        }
        if (msg.what == 1) {
            disPlay("登录成功");
            LoginEntity loginEntity = (LoginEntity) msg.obj;
            App.number = phone.getText().toString();
            App.token = loginEntity.getData().getToken();
            App.id = loginEntity.getData().getMmId();
            App.isLogin = true;
            // App.oldPhoneNumber = loginEntity.getData().get().get(0);
            SharedPreferencesUtils.setParam(getActivity(), "islogin", true);
            SharedPreferencesUtils.setParam(getActivity(), "number", phone.getText().toString());
            // SharedPreferencesUtils.setParam(getActivity(), "oldNumber", loginEntity.getData().getPhones().get(0));
            SharedPreferencesUtils.setParam(getActivity(), "token", loginEntity.getData().getToken());
            SharedPreferencesUtils.setParam(getActivity(), "id", loginEntity.getData().getMmId());
            App.token = loginEntity.getData().getToken();
            App.initpush();
            loginSucess();
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
